package com.etbsms.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etbsms.domain.CodeCountry;
import com.etbsms.domain.CodePlant;
import com.etbsms.domain.ReportProblem;
import com.etbsms.domain.Role;
import com.etbsms.domain.Users;
import com.etbsms.form.QueryAAFForm;
import com.etbsms.form.QueryUsersForm;
import com.etbsms.form.UsersForm;
import com.etbsms.security.User;
import com.etbsms.service.IAgricultureAndFoodService;
import com.etbsms.service.ICodeCountryService;
import com.etbsms.service.ICodeLanguageService;
import com.etbsms.service.ICodePlantService;
import com.etbsms.service.IReportProblemService;
import com.etbsms.service.IRoleService;
import com.etbsms.service.IUsersService;
import com.etbsms.util.Constants;

@Controller
@RequestMapping(value = "/admin/**")
@SuppressWarnings("unused")
public class AdminController {

	@Autowired
	private IRoleService roleService;

	@Autowired
	private IUsersService usersService;
	
	@Autowired
	private IAgricultureAndFoodService agricultureAndFoodService;
	
	@Autowired
	private ICodeLanguageService codeLanguageService;	
	
	@Autowired
	private ICodeCountryService codeCountryService;

	@Autowired
	private ICodePlantService codePlantService;

	@Autowired
	private IReportProblemService reportProblemService;
	
	public ModelMap setMainMenu(ModelMap map){
		map.addAttribute("MAINMENU", Constants.MAIN_MENU_ADMIN);
		return map;
	}

	@RequestMapping(value = "/admin/page/user/add.htm")
	public String pageUserAdd(HttpServletRequest req, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();		
		List<Role> roles = roleService.getAll();		
		map.addAttribute("ITEMS_ROLE",roles);		
		map.addAttribute("UsersForm", new UsersForm(roles));		
		map.addAttribute("SUBMENU", 1);
		setMainMenu(map);	
		return Constants.PAGE_USERADD;
	}
	
	@RequestMapping(value = "/admin/page/user/mdy.htm")
	public String pageUserMdy(HttpServletRequest req, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		map.addAttribute("SUBMENU", 2);
		setMainMenu(map);	
		map.addAttribute("QueryForm", new QueryUsersForm());		
		return Constants.PAGE_USERMDY;
	}
	
	@RequestMapping(value = "/admin/page/report.htm")
	public String pageReport(HttpServletRequest req, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();		
		List<ReportProblem> report = reportProblemService.getAll(Constants.REPORT_TYPE.all);		
		map.addAttribute("REPORT_LIST",report);		
		map.addAttribute("SUBMENU", 3);
		setMainMenu(map);	
		return Constants.PAGE_REPORT;
	}

	@RequestMapping(value = "/admin/page/formview.htm")
	public String pageFormsSelect(HttpServletRequest req, ModelMap map) {
		System.out.println("> " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		setMainMenu(map);	
		map.addAttribute("SUBMENU", 4);
		map.addAttribute("CodeLanguage", codeLanguageService.getAll());
		map.addAttribute("FormUsers", agricultureAndFoodService.getInputUsers());
		
		QueryAAFForm form = new QueryAAFForm(users.getRow_per_page());
		form.setFlow(Constants.FLOW_KENI);
		map.addAttribute("QueryAAFForm", form);
		return Constants.PAGE_ADMINFORMSEL;
	}
	
	
	@RequestMapping(value = "/admin/page/code/country.htm")
	public String pageCodeCountry(HttpServletRequest req, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		map.addAttribute("SUBMENU", 5);		
		setMainMenu(map);	
		map.addAttribute("CodeCountry",codeCountryService.getAll());
		map.addAttribute("CodeCountryForm", new CodeCountry());
		return Constants.PAGE_CODECNY;
	}
	
	@RequestMapping(value = "/admin/code/country/add.htm")
	@ResponseBody
	public boolean codeCountryAdd(CodeCountry form) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		form.setParent(codeCountryService.get(form.getParent_code()));		
		return codeCountryService.save(form);
	}	
	
	@RequestMapping(value = "/admin/code/plant/add.htm")
	@ResponseBody
	public String codePlantAdd(CodePlant form) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		form.setParent(codePlantService.get(form.getParent_code()));		
		String max = codePlantService.getMaxSeqNo();
		form.setCode(max);		
		return codePlantService.save(form) ? form.getCode() : null;
	}	
	
	@RequestMapping(value = "/admin/page/code/plant.htm")
	public String pageCodePlant(HttpServletRequest req, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		map.addAttribute("SUBMENU", 6);		
		setMainMenu(map);	
		
		map.addAttribute("CodePlant",codePlantService.getAll2());
		map.addAttribute("CodePlantForm", new CodePlant());
		
		return Constants.PAGE_CODEPLT;
	}
	
	@RequestMapping(value = "/admin/page/code/language.htm")
	public String pageCodeLanguage(HttpServletRequest req, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		map.addAttribute("SUBMENU", 5);	
		setMainMenu(map);		
		return Constants.PAGE_CODELNG;
	}
	
	
	@RequestMapping(value = "/admin/user/add.htm")
	@ResponseBody
	public boolean userAdd(UsersForm form) {
		
		return usersService.save(form);		
	}
	
	@RequestMapping(value = "/admin/user/select.htm")
	public String userSelect(QueryUsersForm form ,ModelMap map) {		
		if((form.getNowPage() > form.getTotalPages()) && form.getTotalPages() != 0)
			form.setNowPage(form.getTotalPages());	
		form = usersService.query(form);	
		List<Role> rList = roleService.getAll();		
		for(Users _u : form.getObj()){
			Map<Role,Boolean> rMap = new HashMap<Role,Boolean>();
			for(Role _r : rList)
				rMap.put(_r, _u.getRole().contains(_r));			
			_u.setRoleMap(rMap);
		}
		map.addAttribute("_QueryForm", form);
		return Constants.LIST_USERMDY;
	}
	
	@RequestMapping(value = "/admin/user/del.htm")
	@ResponseBody
	public void userDelete(String seq_noArr) {
		for(String seq_no : seq_noArr.split(","))
			usersService.delete(Integer.parseInt(seq_no));
	}
	
	@RequestMapping(value = "/admin/report/query.htm")
	public String reportQuery(@RequestParam(value="type")String type, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();		
		Constants.REPORT_TYPE rType = Constants.REPORT_TYPE.all;
		
		if(type.equalsIgnoreCase("done")){
			rType = Constants.REPORT_TYPE.done;
		}else if(type.equalsIgnoreCase("notyet")){
			rType = Constants.REPORT_TYPE.notyet;			
		}		
		
		List<ReportProblem> report = reportProblemService.getAll(rType);		
		map.addAttribute("REPORT_LIST",report);		
		return Constants.PAGE_REPORTLST;
	}
	
	@RequestMapping(value = "/admin/user/modify.htm")
	@ResponseBody
	public boolean userModify(UsersForm form) {		
//		roleService.g
		Set<Role> set = new HashSet<Role>();
		for(String _rName : form.getRoleItems().split(",")){
			set.add(roleService.get(_rName));
		}
		
		
		Users users = usersService.get(form.getSeq_no());
		users.setFullname(form.getFullname());
		users.setEmail(form.getEmail());
		users.setRole(set);
		return usersService.update(users);			
	}
	
	
	@RequestMapping(value = "/admin/user/exist.htm")
	@ResponseBody
	public boolean isUserExist(String username) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		return usersService.existUsername(username);
	}
	
}
